package com.fqy.thread03.threadpool;

import java.util.concurrent.*;

/**
 * User: fqy
 * Date: 2021/9/4
 * Time: 20:09
 * Describe: 自定义线程池 强制并且常用的使用方法
 *           1.体会拒绝策略
 *           2.体会5大参数
 *           3.生产中如设置合理线程池大小的设置
 *           4.线程池底层工作原理
 *  模拟银行办理业务
 *
 *
 */
public class CustomThreadPool {
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()//该策略默默地丢弃无法处理的任务，不予任何处理也不抛出异常
//                new ThreadPoolExecutor.CallerRunsPolicy()//将某些任务回退到调用者 main	 业务员给第：9	 顾客办理业务
//                new ThreadPoolExecutor.AbortPolicy()//超过RejectedExecutionException
        );

        try {
            for (int i = 0; i < 9; i++) {
                int finalI = i+1;
                threadPool.submit(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 业务员给第："+ finalI +"\t 顾客办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
